package com.silverhaze.diamond.client.data.dao;

import com.silverhaze.diamond.client.data.SpecificData;
import com.silverhaze.diamond.common.entity.*;

import java.util.*;

/**
 * SystemUser: Babanin
 * Date: 24.07.2008
 * Time: 19:32:39
 */
public final class CachingEngine {
    private final Map<SystemUser, List<Service>> userToServices = new HashMap<SystemUser, List<Service>>();
    private final Map<Customer, List<Service>> customerToServices = new HashMap<Customer, List<Service>>();
    private final Map<Detail, List<Service>> detailToServices = new HashMap<Detail, List<Service>>();

    CachingEngine() {
    }

    public void add(Customer c, List<Service> services) {
        customerToServices.put(c, services);
    }


    public void add(Detail d, List<Service> services) {
        detailToServices.put(d, services);
    }

    public void add(SystemUser u, List<Service> services) {
        userToServices.put(u, services);
    }

    public void addService(Service service) {
        SystemUser serUser = service.getSystemUser();
        Customer serCustomer = service.getOwner();

        get(serUser).add(service);
        get(serCustomer).add(service);

        addServiceToDetails(service);
    }

    public void addServiceToDetails(Service service) {
        if (!service.isServiceComplete()) {
            return;
        }

        for (BrokenDetail brokenDetail : service.getBrokenDetailes()) {
            List<Service> serviceList = get(brokenDetail.getDetail());

            if (!serviceList.contains(service)) {
                serviceList.add(service);
            }
        }

        for (UsedDetail usedDetail : service.getUsedDetailes()) {
            List<Service> serviceList = get(usedDetail.getDetail());

            if (!serviceList.contains(service)) {
                serviceList.add(service);
            }
        }
    }

    public void removeService(Service service) {
        SystemUser serUser = service.getSystemUser();
        Customer serCustomer = service.getOwner();

        if (service.isServiceComplete()) {
            Set<Detail> details = getDetailsFromService(service);

            for (Detail detail : details) {
                get(detail).remove(service);
            }
        }

        get(serUser).remove(service);
        get(serCustomer).remove(service);
    }

    public Set<Detail> getDetailsFromService(Service service) {
        if (service.isServiceComplete()) {
            Set<Detail> result = new HashSet<Detail>();

            for (BrokenDetail brokenDetail : service.getBrokenDetailes()) {
                result.add(brokenDetail.getDetail());
            }

            for (UsedDetail usedDetail : service.getUsedDetailes()) {
                result.add(usedDetail.getDetail());
            }

            return result;
        }

        return null;
    }

    public List<Service> get(Customer customer) {
        if (customer != null) {
            List<Service> services = customerToServices.get(customer);

            if (services == null) {
                services = SpecificData.getServicesBy(customer);
                add(customer, services);
            }

            return services;
        }

        return null;
    }

    public List<Service> get(SystemUser user) {
        if (user != null) {
            List<Service> services = userToServices.get(user);

            if (services == null) {
                services = SpecificData.getServicesBy(user);
                add(user, services);
            }

            return services;
        }

        return null;
    }

    public List<Service> get(Detail detail) {
        if (detail != null) {
            List<Service> services = detailToServices.get(detail);

            if (services == null) {
                services = SpecificData.getServicesBy(detail);
                add(detail, services);
            }

            return services;
        }

        return null;
    }

}

